package com.lyp.qczp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.lyp.qczp.dao.Remuneradao;
import com.lyp.qczp.entity.Remunera;
@Service
public class RemuneraServiceImp implements RemuneraService{
	@Autowired
	private Remuneradao remuneradao;
	
	@Override
	public int save(Remunera remunera) {
		Remunera save = remuneradao.save(remunera);
		if (save!=null) {
			return 1;
		}
		return 0;
	}

	@Override
	public int deleteById(Integer reId) {
		remuneradao.deleteById(reId);
		return 1;
	}

	@Override
	public Map<String, Object> findAll(PageRequest pageable, Remunera remunera) {
		Page<Remunera> findAll = remuneradao.findAll(selectquanpage(remunera),pageable);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", findAll.getContent());
		map.put("total", findAll.getTotalElements());
		return map;
	}
	//条件查询Specification
			private Specification<Remunera> selectquanpage(Remunera remunera){
				return new Specification<Remunera>() {

					/**
					 * 
					 */
					private static final long serialVersionUID = 1L;

					@Override
					public Predicate toPredicate(Root<Remunera> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
						// TODO Auto-generated method stub
						Predicate conjunction = cb.conjunction();
						List<Expression<Boolean>> expressions = conjunction.getExpressions();
						if (remunera.getReName()!=null&&remunera.getReName()!="") {
							expressions.add(cb.like(root.<String>get("reName"), "%"+remunera.getReName()+"%"));
						}
						if (remunera.getReId()!=null&&remunera.getReId()!=0) {
							expressions.add(cb.equal(root.<Integer>get("reId"), remunera.getReId()));
						}
						
						return conjunction;
					}
				};
			}
}
